import axios from 'axios'
import { Notify, LoadingBar } from 'quasar'

const service = axios.create({
    baseURL: '/api',
    timeout: 5000,
})

service.interceptors.request.use(
    config => {
        LoadingBar.start()
        config.headers['Authorization'] = 'my token'
        if (config.method === 'post' || config.method === 'put') {
            for (let key in config.data) {
                config.data[key] = config.data[key] || null
            }
        }
        return config
    },
    error => {
        LoadingBar.stop()
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        LoadingBar.stop()
        if (response.data instanceof ArrayBuffer) {
            return response
        }
        const res = response.data
        if (res && res.code === 0) {
            return res
        } else {
            if (res && res.code === 4001) {
                router.push('/login')
                Notify.create({
                    position: 'top',
                    message: res.data || '您还未登录或登录超时，请重新登录',
                    color: 'negative',
                    icon: 'report_problem',
                })
            } else {
                Notify.create({
                    position: 'top',
                    message: res.data || '请求服务器错误，请稍后再试或联系系统管理员',
                    color: 'negative',
                    icon: 'report_problem',
                })
            }
        }
        // return response.data
    },
    error => {
        LoadingBar.stop()
        if (error.code === 'ECONNABORTED') {
            Notify.create({
                position: 'top',
                message: '请求服务器超时',
                color: 'negative',
                icon: 'report_problem',
            })
        } else {
            switch (error.response.status) {
                case 401:
                    router.push({ name: 'Login' })
                    Notify.create({
                        position: 'top',
                        message: '您还没有登录，或登录超时，请登录',
                        color: 'negative',
                        icon: 'report_problem',
                    })
                    break
                case 403:
                    // router.push({ name: 'Forbidden' })
                    Notify.create({
                        position: 'top',
                        message: '您无权访问此接口，请稍后再试或联系系统管理员',
                        color: 'negative',
                        icon: 'report_problem',
                    })
                    break
                case 404:
                    // router.push({ name: 'NotFound' })
                    Notify.create({
                        position: 'top',
                        message: '访问的接口不存在，请稍后再试或联系系统管理员',
                        color: 'negative',
                        icon: 'report_problem',
                    })
                    break
                case 500:
                    // router.push({ name: '500' })
                    Notify.create({
                        position: 'top',
                        message: '请求服务器错误，请稍后再试或联系系统管理员',
                        color: 'negative',
                        icon: 'report_problem',
                    })
                    break
                default:
                    // router.push({ name: '500' })
                    Notify.create({
                        position: 'top',
                        message: '请求服务器错误，请稍后再试或联系系统管理员',
                        color: 'negative',
                        icon: 'report_problem',
                    })
            }
        }
        // return Promise.reject(error)
    }
)

export default service
